package com.zzyl.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeviceDataServiceImpl implements DeviceDataService {
    @Autowired
    private DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> getDeviceDataPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        // 处理一下时间数据
        deviceDataPageReqDto.setStartDate(LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime()));
        deviceDataPageReqDto.setEndDate(LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime()));
        // 分页查询
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        Page<DeviceDataVo> page = deviceDataMapper.selectByPage(deviceDataPageReqDto);
        PageResponse<DeviceDataVo> deviceDataVoPageResponse = PageResponse.of(page, DeviceDataVo.class);
        return deviceDataVoPageResponse;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, Long endTime, String functionId, Long startTime)
    {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> dataGraphVos = deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, startDate, endDate);
        // 获取0~23点的所有数据
        List<DeviceDataGraphVo> dataGraphVos1 = DeviceDataGraphVo.dayInstance(startDate);
        for (DeviceDataGraphVo dataGraphVo : dataGraphVos) {
            for (DeviceDataGraphVo deviceDataGraphVo : dataGraphVos1) {
                if(deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime()) ){
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                }
            }
        }
        return dataGraphVos1;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, Long endTime, String functionId, Long startTime) {
        LocalDateTime startDate = LocalDateTimeUtil.of(startTime);
        LocalDateTime endDate = LocalDateTimeUtil.of(endTime);
        List<DeviceDataGraphVo> dataGraphVos = deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, startDate, endDate);
        // 获取0~23点的所有数据
        List<DeviceDataGraphVo> dataGraphVos1 = DeviceDataGraphVo.weekInstance(startDate);
        for (DeviceDataGraphVo dataGraphVo : dataGraphVos) {
            for (DeviceDataGraphVo deviceDataGraphVo : dataGraphVos1) {
                if(deviceDataGraphVo.getDateTime().equals(dataGraphVo.getDateTime()) ){
                    deviceDataGraphVo.setDataValue(dataGraphVo.getDataValue());
                }
            }
        }
        return dataGraphVos1;

    }
}
